package pl.ivmx.cinema;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCConnection {
	
	private static JDBCConnection instance ;
	private Connection connection;
	
	synchronized public ResultSet executeQuery(String query){
		Statement stmt;
		ResultSet rs = null;
		try {
				stmt = connection.createStatement();
				  rs = stmt.executeQuery(query);
				
		}
      catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

    
      return rs;
	}
	
	synchronized public boolean executeStatement(String query){
		Statement stmt;
	
		try {
			stmt = connection.createStatement();
			  return stmt.execute(query);
			
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	private JDBCConnection(){
		
		try {
            //String url = "jdbc:msql://200.210.220.1:1114/Demo";
			//   jdbc:oracle:thin:[<user>/<password>]@//<host>[:<port>]/<service>

			String url = "jdbc:oracle:thin:student03/student@//10.1.1.201:1521/BILING";
			System.out.println(url);

			connection = DriverManager.getConnection(url,"student03","student");
//            Statement stmt = connection.createStatement();
//            ResultSet rs;
// 
//            rs = stmt.executeQuery("SELECT title FROM Movie");
//            while ( rs.next() ) {
//                String lastName = rs.getString("title");
//                System.out.println(lastName);
//            }
//            conn.close();
        } catch (Exception e) {
            System.err.println("Got an exception! " );
            System.err.println(e.getMessage());
            e.printStackTrace();
        } 
		
		
	};
	
	synchronized public static JDBCConnection getInstance(){
		if (instance == null)
			instance = new JDBCConnection();
		return instance;
	}

	public Connection getConnection() {
		return connection;
	}
	
	public void closeConnection(){
		try {
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	

}
